"""
测试用例操作步骤：
1、进入百度，输入王长帅
2、点击百度一下
3、点击搜索结果出来的第一个链接
4、清空爱企查输入框
5、输入内容：王长帅榴莲公司
6、点击搜索
7、断言当前 页面内容包含：抱歉，没有找到相关结果…
"""
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
class SearchName:
    def __init__(self,driver):
        self.driver = driver
        self.driver.get("http://www.baidu.com")
        self.driver.maximize_window()
        driver.implicitly_wait(15)
    def input_baidu(self,text:str):
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.ID,"kw"))).send_keys(text)
    def click_search_button(self):
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located((By.TAG_NAME, 'input')))[8].click()
    def click_result_one(self):
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH, "//*[@id='1']/div/div[1]/h3/a"))).click()
    def input_aiqi(self,text:str):
        win = self.driver.window_handles
        self.driver.switch_to.window(win[-1])
        input = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.ID, "aqc-header-search-input")))
        # 先清除文本内容
        input.clear()
        # 再输入内容
        input.send_keys(text)
    def click_search_aiqi(self):
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME,"search-btn"))).click()
    def assert_text_result(self):
        text_result = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME,"text-one"))).text
        print(text_result)
        assert text_result == "抱歉，没有找到相关结果…"
    def quit(self):
        self.driver.quit() #退出浏览器

if __name__ == '__main__':
    driver = webdriver.Chrome()
    bro = SearchName(driver)  # 实例化对象
    bro.input_baidu("王长帅")  # 点击王长帅
    bro.click_search_button() # 点击百度
    bro.click_result_one() #点击百度出来的第一个内容
    bro.input_aiqi("王长帅榴莲公司") # 输入内容
    bro.click_search_aiqi() # 搜索
    bro.assert_text_result() # 断言是否通过
    bro.quit() # 退出浏览器


